package com.sdl.timesblogmp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdl.timesblogmp.mapper.*;
import com.sdl.timesblogmp.model.entity.Blog;
import com.sdl.timesblogmp.model.entity.BlogCate;
import com.sdl.timesblogmp.model.entity.BlogTag;
import com.sdl.timesblogmp.model.vo.BlogInfo;
import com.sdl.timesblogmp.model.vo.DashBoardInfo;
import com.sdl.timesblogmp.model.vo.PageParam;
import com.sdl.timesblogmp.service.BlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sdl
 * @since 2020-05-11
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {
    @Resource
    private BlogMapper blogMapper;
    @Resource
    private BlogCateMapper blogCateMapper;
    @Resource
    private BlogTagMapper blogTagMapper;
    @Resource
    private TagMapper tagMapper;
    @Resource
    private CategoryMapper categoryMapper;
    @Override
    public int postBlog(BlogInfo blogInfo) {

        Blog blog = blogInfo.getBlog();
        int insert = blogMapper.insert(blog);
        BlogCate blogCate = new BlogCate(null,blog.getId(),blogInfo.getCategory().getId());
        blogCateMapper.insert(blogCate);
        blogInfo.getTag().stream().forEach(item ->{
            BlogTag blogTag = new BlogTag();
            blogTag.setBlogid(blog.getId());
            blogTag.setTagid(item.getId());
            blogTagMapper.insert(blogTag);
        });
        return insert;
    }

    @Override
    public IPage<BlogInfo> findBlogInfo(PageParam pageParam) {
        if (pageParam.getPageNo()==0||pageParam.getPageSize()==0){
            pageParam.setPageNo(1);
            pageParam.setPageSize(6);
        }
        QueryWrapper<BlogInfo> wrapper = new QueryWrapper();
        if (pageParam.getName()!=null){
            wrapper.like("title",pageParam.getName());
        }
        Page<BlogInfo> page = new Page<>(pageParam.getPageNo(),pageParam.getPageSize());
        IPage<BlogInfo> pageinfo = blogMapper.findBlogInfo(page,wrapper);
      //  pageinfo.setRecords(blogMapper.findBlogInfo(pageParam.getName()));
        return pageinfo;
    }

    @Override
    public BlogInfo findOneBlogInfo(Integer id) {
        return blogMapper.findOneBlogInfo(id);
    }

    @Override
    public DashBoardInfo getDashBoardInfo() {
        Integer blogs = blogMapper.selectCount(null);
        Integer categorys = categoryMapper.selectCount(null);
        Integer tags = tagMapper.selectCount(null);
        DashBoardInfo dashBoardInfo = new DashBoardInfo(blogs,categorys,tags);
        return dashBoardInfo;
    }
}
